// pages/decovery/index.js
const common = require('../../public.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selected:1,
    pageSize: 10,
    pageNum: 1,
    articleList: [],
    canLoad: true,
    isLoadingBusy: false,
    refreshing: false,
    typeList:[],
    active:0,
    actives:0,
    userInfo:{},
  },

  /**
   * 生命周期函数--监听页面加载
   */
onLoad(){
  console.log("进来了")
  this.typeCatory()  //分类
},
  //  分类
  typeCatory(){
   var that = this
    common.get('/storeArticle/getStoreArticleCat', {}, function (res) {
      if (res.status === 200) {
        var obj = {name:'全部',id:''}
        res.data.length>0 ? res.data.unshift(obj) :res.data.unshift(obj)
        that.setData({
        typeList:res.data
       },()=>{
       
        that.getStoreArticleList(true,that.data.typeList[0].id)   //分类
       })
      } else {
        common.showRequestErrMsg(res)
      }
    })
  },
  // 发现店铺
  onChange(event) { 
    event.detail.name==0 ? event.detail.name='' : event.detail.name
    this.setData({
      actives: event.detail.name,
    })
    this.getStoreArticleList(true,event.detail.name)
  },
  getStoreArticleList(refresh = false,id) {
    const that = this
    if (refresh) {
      this.data.canLoad = true
      this.setData({
        refreshing: true
      })
    }
    if (that.data.canLoad) { //可以加载
      if (!refresh && this.data.isLoadingBusy) {
        return
      }
      this.setData({
        isLoadingBusy: true
      })
      const pageNum = refresh ? 1 : this.data.pageNum + 1
      let tempJson = {
        pageSize: that.data.pageSize,
        pageNum,
        userId:this.data.userInfo.id,
        catId:id
      }
      common.post('/storeArticle/getStoreArticleList', tempJson, function (res) {
        if (res.status === 200) {
          that.data.pageNum = pageNum
          if (res.data != '') {
            that.setData({
              articleList: refresh ? res.data : [...that.data.articleList, ...res.data]
            })
          } else {
           // 两种情况的空1.切换的空2.下滑的空
            if(pageNum==1){
              that.setData({
                articleList:[]
              })
              
              return
            }
            that.setData({
              canLoad: false,
            })
          }
        } else {
          common.showRequestErrMsg(res)
        }
      }, function () {
        that.setData({
          isLoadingBusy: false
        })
        if (refresh) {
          //停止下拉刷新
          wx.stopPullDownRefresh()
          that.setData({
            refreshing: false
          })
        }
      })
    }
  },
  toArticleDetail(e) {
    const item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '/pages/discovery/articleDetail/index?id=' + item.id
        + '&storeName=' + item.storeName
        + '&title=' + item.title
        + '&createTime=' + item.createTime
        + '&storeImg=' + item.storeImg
    })
  },
  onShow(){
    const that = this
    common.getLocalUserInfo(function (success, data) {
      if (success) {
        that.setData({
          userInfo: data.user,
          token: data.token
        }, function () {
       
          this.read_message()
        })
      } else {
        that.setData({
          userInfo: null,
          token: '',
        })
      }
    },true)
  
  },
   // 统计用户未读消息
   read_message() {
    var that = this
    var tmp = {
      userId: this.data.userInfo.id,
      token: this.data.token
    }
    common.get('/alipush/getUserNotReadMessageCount', tmp, res => {
      if (res.status === 200) {
        // if(res.data>0){
        //   wx.showTabBarRedDot({
        //     index: 3,
        //   }) 
        //   return
        // }
        // wx.hideTabBarRedDot({
        //   index: 3,
        // })
      } else {
        common.showRequestErrMsg(res)
      }

    })
  },
  // onReachBottom() {
    
  // },

  loadMore(){
    var id = this.data.actives
    if(id==0){
       id=''
    }
    this.getStoreArticleList(false,id)
  },
  onPullDownRefresh: function () {
    var id = this.data.actives
    if(id==0){
       id=''
    }
    this.getStoreArticleList(true,id)
  },
  onReady:function(){
    setTimeout(() => {
      this.setData({
        sho:true
      })
     }, 1000);
  },

})